<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="目录名称" prop="listName">
        <el-input
          v-model="queryParams.listName"
          placeholder="请输入目录名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select class="filter-item" v-model="queryParams.status" placeholder="请选择审核状态">
          <el-option v-for="item in attrTypeAuditOptions" :key="item.dictCode" :label="item.dictLabel"
                     :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="删除状态" prop="isDel" v-if="false">
        <el-select class="filter-item" v-model="queryParams.isDel" placeholder="请选择删除状态">
          <el-option v-for="item in attrTypeDeleOptions" :key="item.dictCode" :label="item.dictLabel"
                     :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['receipt:knowledgeList:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['receipt:knowledgeList:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['receipt:knowledgeList:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['receipt:knowledgeList:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="目录名称" align="center" prop="listName" />
      <el-table-column label="知识点个数" align="center" prop="managerNum" v-if="false"/>
      <el-table-column label="审核状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="删除状态" align="center" prop="isDel" :formatter="isDelFormat" v-if="false" />
      <el-table-column label="审核意见" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="false">
           <template slot-scope="scope">
               <el-button
                 size="mini"
                 type="text"
                 icon="el-icon-edit"
                 @click="handleUpdate(scope.row)"
                 v-hasPermi="['receipt:knowledgeList:edit']"
               >修改</el-button>
                 <el-button
                   size="mini"
                   type="text"
                   icon="el-icon-delete"
                   v-show="isDeleteButtonShow(scope.row)"
                   @click="handleDelete(scope.row)"
                   v-hasPermi="['receipt:knowledgeList:remove']"
                 >删除</el-button>
           </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识点目录管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="目录名称" prop="listName">
          <el-input v-model="form.listName" placeholder="请输入目录名称" />
        </el-form-item>
        <el-form-item label="父级目录编号" prop="parentId" v-if="false">
          <el-input v-model="form.parentId" placeholder="请输入父级目录编号" />
        </el-form-item>
        <el-form-item label="审核状态" prop="status" v-show="false">
          <el-select v-model="form.status" placeholder="请选择审核状态 1未审核(默认) 2已审核 3驳回">
            <el-option
              v-for="dict in attrTypeAuditOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除状态" prop="isDel" v-show="false">
          <el-select v-model="form.isDel" placeholder="请选择删除状态 0未删除(默认) 1已删除">
            <el-option
              v-for="dict in attrTypeDeleOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="remark" v-show="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listList, getList, delList, delListLogic,duplicateValidation, addList, updateList, exportList } from "@/api/knowledge/knowledgeList";
import { listManager, getManager, delManager, delManagerReal, addManager, updateManager, exportManager, getTreeData } from "@/api/knowledge/knowledgeManager";
export default {
  name: "List",
  components: {
  },
  data() {
    var duplicateValidations = (rule, value, callback) => {
        //新增的时候去验证知识点目录是否重复
        duplicateValidation(this.form).then(response => {
            //判断是否有重复值
            if (response.data > 0){
                return callback(new Error('知识点目录重复，请重新输入'));
            }else {
                callback();
            }
        });
    };
    return {
      // 遮罩层
      loading: true,
      //数据字典数据删除状态
      attrTypeDeleOptions: [],
      //数据字典审核状态
      attrTypeAuditOptions: [],
      //控制字段的显示/隐藏
      auditStatus: true,
      deleteStatus: true,
      remark: false,
      isIdShow: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识点目录管理表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        knowledgeListId: null,
        pageNum: 1,
        pageSize: 10,
        listName: null,
        status: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          listName:[
              { required: true, //是否必填
                  message: '知识点目录不能为空', //规则
                  trigger: 'blur'  //何事件触发
              },
              //可以设置双重验证标准
              { min: 2, max: 25,  message: '长度在 2 到 25 个字符', },
              { validator: duplicateValidations, trigger: 'blur' },
          ]
      }
    };
  },
  created() {
    this.getList();
    //通过数据字典获取删除状态
    this.getDicts('sys_delete_status').then(response => {
        this.attrTypeDeleOptions = response.data;
    });
    //通过数据字典获取审核状态
    this.getDicts('sys_audit_status').then(response => {
        this.attrTypeAuditOptions = response.data;
    });
  },
  methods: {
    /** 查询知识点目录管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.isDel = '0';
      listList(this.queryParams).then(response => {
        this.listList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 审核状态 1未审核(默认) 2已审核 3驳回字典翻译
    statusFormat(row, column) {
        return this.selectDictLabel(this.attrTypeAuditOptions, row.status);
    },
    // 删除状态 0未删除(默认) 1已删除字典翻译
    isDelFormat(row, column) {
        return this.selectDictLabel(this.attrTypeDeleOptions, row.isDel);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //控制删除按钮显示 已审核不能显示删除 这里已审核且目录下面没有知识点的也可以删除
    isDeleteButtonShow(row){
        if (row.status == '2'){
            //查询目录下有没有知识点
            if (row.managerNum > 0){
                return false;
            }else {
                return true;
            }
        }else {
            return true;
        }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        listName: null,
        parentId: null,
        status: 0,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.auditStatus = false;
      this.deleteStatus = false;
      this.form.status = 1;
      this.form.isDel = 0;
      //现在默认只有一级目录parentId都是0 后期有多级目录这里需要修改
      this.form.parentId = 0;
      this.open = true;
      this.title = "添加知识点目录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getList(id).then(response => {
        this.form = response.data;
        this.form.status = '1';
        this.form.remark = '';
        this.auditStatus = true;
        this.deleteStatus = true;
        this.open = true;
        this.title = "修改知识点目录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateList(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除知识点目录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delListLogic(ids);
        }).then(response => {
          if (response.data > 0){
              this.msgSuccess("请先删除目录下的知识点再删除目录");
          }else {
              this.getList();
              this.msgSuccess("删除成功");
          }
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有知识点目录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportList(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
